package com.sheliduan.itlearning.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sheliduan.itlearning.common.domain.dos.CategoryDO;
import com.sheliduan.itlearning.common.domain.dos.OrderDO;
import com.sheliduan.itlearning.common.domain.dos.UserBoughtCourseDO;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: 断舍离
 * @date: 2025/2/10 10:12
 * @description: 订单映射接口
 **/
public interface OrderMapper extends BaseMapper<OrderDO> {

    /*
     * 根据查询条件分页查询分类数据
     * */
    default Page<OrderDO> selectPageList(long current, long size, Integer userId){
        // MyBatis分页对象, 需传入当前页码和每页记录数
        Page<OrderDO> page = new Page<>(current, size);
        LambdaQueryWrapper<OrderDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderDO::getUserId, userId)
                .orderByDesc(OrderDO::getOrderTime);
        return selectPage(page, wrapper);
    }

    /*
     * 查询博主下的所有订单
     * */
    default List<OrderDO> selectList(List<Integer> courseIds, String number, LocalDateTime startDate, LocalDateTime endDate){
        if(courseIds == null || courseIds.isEmpty()) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<OrderDO> wrapper = new LambdaQueryWrapper<>();
        wrapper
                .in(OrderDO::getCourseId, courseIds)
                .like(StringUtils.isNotBlank(number), OrderDO::getNumber, number)
                .ge(Objects.nonNull(startDate), OrderDO::getOrderTime, startDate) // 大于等于 startDate
                .le(Objects.nonNull(endDate), OrderDO::getOrderTime, endDate)// 小于等于 endDate
                .orderByDesc(OrderDO::getOrderTime);
        return selectList(wrapper);
    }

}
